import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  ManyToMany,
  ManyToOne,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  JoinTable,
} from 'typeorm';
import { IsOptional, IsString, MaxLength, IsNotEmpty } from 'class-validator';
import { Type } from 'class-transformer';
import { CrudValidationGroups } from '@nestjsx/crud';
import { User } from '@/modules/users/entities/user.entity';
import { Song } from './song.entity';

const { CREATE, UPDATE } = CrudValidationGroups;

@Entity({ name: 'play_lists' })
export class PlayList {
  @IsOptional({ always: true })
  @PrimaryGeneratedColumn()
  id: number;

  @IsOptional({ groups: [UPDATE] })
  @IsNotEmpty({ groups: [CREATE] })
  @IsString({ always: true })
  @MaxLength(200, { always: true })
  @Index()
  @Column({ type: 'character varying', length: 200, comment: '歌单名' })
  title: string;

  @IsOptional({ always: true })
  @IsString({ always: true })
  @MaxLength(1000, { always: true })
  @Column({ type: 'character varying', nullable: true, comment: '歌单简介' })
  description: string;

  @IsOptional({ always: true })
  @IsString({ each: true, always: true })
  @MaxLength(20, { each: true, always: true })
  @Column({
    type: 'character varying',
    length: 20,
    array: true,
    nullable: true,
    comment: '标签',
  })
  tags: string[];

  @Type(_type => Song)
  @ManyToMany(
    _type => Song,
    song => song.inPlayLists,
  )
  @JoinTable()
  songs: Song[];

  @Type(_type => User)
  @ManyToOne(
    _type => User,
    user => user.playLists,
  )
  belongsToUser: User;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @DeleteDateColumn()
  deletedAt: Date;
}
